package com.rephilo.service.impl;

import com.rephilo.annotation.Autowired;
import com.rephilo.annotation.Service;
import com.rephilo.annotation.Transactional;
import com.rephilo.dao.AccountDao;
import com.rephilo.pojo.Account;
import com.rephilo.service.TransferService;

import java.sql.SQLException;

/**
 * @author 应癫
 */
@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountDao accountDao;

    @Transactional(rollbackFor = SQLException.class)
    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        Account from = accountDao.queryAccountByCardNo(fromCardNo);
        Account to = accountDao.queryAccountByCardNo(toCardNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        accountDao.updateAccountByCardNo(to);
        accountDao.updateAccountByCardNo(from);

//        throw new SQLException("手动异常");
    }
}
